<?php
/**
 * YICMS
 * ============================================================================
 * 版权所有 2014-2017 YICMS，并保留所有权利。
 * 网站地址: http://www.yicms.vip
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Created by PhpStorm.
 * Author: kenuo
 * Date: 2017/11/17
 * Time: 下午4:38
 */

namespace App\Services\TotalData;

use App\Repositories\TotalData\StatRepository;
use \Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Auth;

class StatService
{
    /**
     * ActionLogsService constructor.
     * @param $actionLogsRepository
     */
    public function __construct(StatRepository $statRepository)
    {
        $this->statRepository = $statRepository;
    }

    /**
     * 获取 渠道统计信息
     */
    public function getChannelStat($request, &$sum)
    {
        //新增用户
        $newUsers = $this->statRepository->getChannelNewUsers($request)->toArray();
        $newUsers = array_combine(array_column($newUsers, 'key'), $newUsers);

        //活跃用户
        $activeUsers = $this->statRepository->getChannelActiveUsers($request)->toArray();
        $activeUsers = array_combine(array_column($activeUsers, 'key'), $activeUsers);

        //活跃付费用户
        $payUsers = $this->statRepository->getChannelPayUsers($request)->toArray();
        $payUsers = array_combine(array_column($payUsers, 'key'), $payUsers);

        //总收入
        $totalAmounts = $this->statRepository->getChannelTotalAmounts($request)->toArray();
        $totalAmounts = array_combine(array_column($totalAmounts, 'key'), $totalAmounts);

        foreach ($activeUsers as $key => $value) {
            $activeUsers[$key]['new_users'] = isset($newUsers[$key]) ? $newUsers[$key]['new_users'] : 0;
            $sum['new_users'] += $activeUsers[$key]['new_users'];

            $activeUsers[$key]['total_amounts'] = isset($totalAmounts[$key]) ? $totalAmounts[$key]['total_amounts'] : 0;
            $sum['total_amounts'] += $activeUsers[$key]['total_amounts'];

            $activeUsers[$key]['pay_users'] = isset($payUsers[$key]) ? $payUsers[$key]['pay_users'] : 0;
            $sum['pay_users'] += $activeUsers[$key]['pay_users'];

            $sum['active_users'] += $activeUsers[$key]['active_users'];

            //活跃付费率=活跃付费用户/活跃用户
            $activeUsers[$key]['active_pay_rate'] = (($activeUsers[$key]['active_users'] > 0) ? round($activeUsers[$key]['pay_users'] / $activeUsers[$key]['active_users'] * 100, 2) . '%' : 0);
            //ARPU=总收入/活跃用户
            $activeUsers[$key]['arpu'] = (($activeUsers[$key]['active_users'] > 0) ? round($activeUsers[$key]['total_amounts'] / $activeUsers[$key]['active_users'], 2) : 0);
            //ARPPU=总收入/活跃付费用户
            $activeUsers[$key]['arppu'] = (($activeUsers[$key]['pay_users'] > 0) ? round($activeUsers[$key]['total_amounts'] / $activeUsers[$key]['pay_users'], 2) : 0);
        }

        $sum['active_pay_rate'] = $sum['active_users'] > 0 ? (round($sum['pay_users'] / $sum['active_users'], 4) * 100) . '%' : 0;
        $sum['arpu'] = $sum['active_users'] > 0 ? round($sum['total_amounts'] / $sum['active_users'], 2) : 0;
        $sum['arppu'] = $sum['pay_users'] > 0 ? round($sum['total_amounts'] / $sum['pay_users'], 2) : 0;

        return $activeUsers;
    }
}